home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdlib / tst-strtol.c < prev    next >
C/C++ Source or Header  |  1993-03-17  |  3KB  |  128 lines

  1. /* My bet is this was written by Chris Torek.
  2.    I reformatted and ansidecl-ized it, and tweaked it a little.  */
  3.  
  4. #include <ansidecl.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <strings.h>
  10.  
  11. struct ltest
  12.   {
  13.     CONST char *str;        /* Convert this.  */
  14.     unsigned long int expect;    /* To get this.  */
  15.     int base;            /* Use this base.  */
  16.     char left;            /* With this left over.  */
  17.     int err;            /* And this in errno.  */
  18.   };
  19. static CONST struct ltest tests[] =
  20.   {
  21.     /* First, signed numbers.  */
  22.     { "   -17",        -17,        0,    0,    0 },
  23.     { " +0x123fg",    0x123f,        0,    'g',    0 },
  24.     { "2147483647",    2147483647,    0,    0,    0 },
  25.     { "2147483648",    2147483647,    0,    0,    ERANGE },
  26.     { "214748364888",    2147483647,    0,    0,    ERANGE },
  27.     { "2147483650",    2147483647,    0,    0,    ERANGE },
  28.     { "-2147483649",    -2147483648,    0,    0,    ERANGE },
  29.     { "-2147483648",    -2147483648,    0,    0,    0 },
  30.     { "0123",        0123,        0,    0,    0 },
  31.     { "0x1122334455z",    2147483647,    16,    'z',    ERANGE },
  32.     { "0x0xc",        0,        0,    'x',    0 },
  33.     { "yz!",        34*36+35,    36,    '!',    0 },
  34.     { NULL,        0,        0,    0,    0 },
  35.  
  36.     /* Then unsigned.  */
  37.     { "  0",        0,        0,    0,    0 },
  38.     { "0xffffffffg",    0xffffffff,    0,    'g',    0 },
  39.     { "0xf1f2f3f4f5",    0xffffffff,    0,    0,    ERANGE },
  40.     { "-0x123456789",    0xffffffff,    0,    0,    ERANGE },
  41.     { "-0xfedcba98",    -0xfedcba98,    0,    0,    0 },
  42.     { NULL,        0,        0,    0,    0 },
  43.   };
  44.  
  45. static void EXFUN(expand, (char *dst, int c));
  46.  
  47. int
  48. DEFUN_VOID(main)
  49. {
  50.   register CONST struct ltest *lt;
  51.   char *ep;
  52.   int status = 0;
  53.  
  54.   for (lt = tests; lt->str != NULL; ++lt)
  55.     {
  56.       register long int l;
  57.  
  58.       errno = 0;
  59.       l = strtol(lt->str, &ep, lt->base);
  60.       printf("strtol(\"%s\", , %d) test %u",
  61.          lt->str, lt->base, (unsigned int) (lt - tests));
  62.       if (l == (long int) lt->expect && *ep == lt->left && errno == lt->err)
  63.     puts("\tOK");
  64.       else
  65.     {
  66.       puts("\tBAD");
  67.       if (l != (long int) lt->expect)
  68.         printf("  returns %ld, expected %ld\n",
  69.            l, (long int) lt->expect);
  70.       if (lt->left != *ep)
  71.         {
  72.           char exp1[5], exp2[5];
  73.           expand(exp1, *ep);
  74.           expand(exp2, lt->left);
  75.           printf("  leaves '%s', expected '%s'\n", exp1, exp2);
  76.         }
  77.       if (errno != lt->err)
  78.         printf("  errno %d (%s)  instead of %d (%s)\n",
  79.            errno, strerror(errno), lt->err, strerror(lt->err));
  80.       status = 1;
  81.     }
  82.     }
  83.  
  84.   for (++lt; lt->str != NULL; lt++)
  85.     {
  86.       register unsigned long int ul;
  87.  
  88.       errno = 0;
  89.       ul = strtoul(lt->str, &ep, lt->base);
  90.       printf("strtoul(\"%s\", , %d) test %u",
  91.          lt->str, lt->base, (unsigned int) (lt - tests));
  92.       if (ul == lt->expect && *ep == lt->left && errno == lt->err)
  93.     puts("\tOK");
  94.       else
  95.     {
  96.       puts("\tBAD");
  97.       if (ul != lt->expect)
  98.         printf("  returns %lu, expected %lu\n",
  99.            ul, lt->expect);
  100.       if (lt->left != *ep)
  101.         {
  102.           char exp1[5], exp2[5];
  103.           expand(exp1, *ep);
  104.           expand(exp2, lt->left);
  105.           printf("  leaves '%s', expected '%s'\n", exp1, exp2);
  106.         }
  107.       if (errno != lt->err)
  108.         printf("  errno %d (%s) instead of %d (%s)\n",
  109.            errno, strerror(errno), lt->err, strerror(lt->err));
  110.       status = 1;
  111.     }
  112.     }
  113.  
  114.   exit(status ? EXIT_FAILURE : EXIT_SUCCESS);
  115. }
  116.  
  117. static void
  118. DEFUN(expand, (dst, c), register char *dst AND register int c)
  119. {
  120.   if (isprint(c))
  121.     {
  122.       dst[0] = c;
  123.       dst[1] = '\0';
  124.     }
  125.   else
  126.     (void) sprintf(dst, "%#.3o", (unsigned int) c);
  127. }
  128.